﻿using System.Web;
using System.Text.RegularExpressions;
using System;
namespace HZ.Web
{
    public static class WebUtil
    {
        /// <summary>
        /// 利用脚本跳转链接
        /// </summary>
        /// <param name="url">需跳转的链接，支持绝地址、相对地址、ASP.NET相对地址</param>
        public static void RedirectByScript(string url)
        {
            if (url.StartsWith("~/"))
            {
                string apppath = HttpContext.Current.Request.ApplicationPath;
                url = apppath + url.Replace("~/", "/");
            }
            string script = string.Format("<script type=\"text/javascript\">location='{0}';</script>", url);
            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 无处理直接提示消息在页面头部显示消息
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <example>
        /// 代码示例
        /// <code> 
        ///     HZ.Web.WebUtil.Alert("测试消息");            
        ///     Response.Write("ABC"); 
        /// </code>
        ///  此时，出现"测试消息"对话框的时候，页面是看不见"ABC"的。只有点击对话框的确定按钮之后，才可以看到"ABC"
        /// </example>
        public static void Alert(string msg)
        {
            string script = string.Format("<script type=\"text/javascript\">alert('{0}');</script>", msg);
            HttpContext.Current.Response.Write(script);
        }
        /// <summary>
        /// 无处理直接提示消息在页面底部显示消息
        /// </summary>
        /// <param name="msg">要提示的消息</param>
        /// <param name="page">this</param>
        /// <example>
        /// 代码示例
        /// <code>
        ///     HZ.Web.WebUtil.Alert("测试消息", this);
        ///     Response.Write("ABCD");
        /// </code>
        /// 此时，出现"测试消息"对话框的时候，页面可以看见"ABCD"的。即提示消息最后执行。
        /// </example>
        public static void Alert(string msg, System.Web.UI.Page page)
        {
            string script = string.Format("<script type=\"text/javascript\">alert('{0}');</script>", msg);
            page.ClientScript.RegisterStartupScript(page.GetType(), "alert", script);
        }


        /// <summary>
        /// 构造Response发送的脚本函数
        /// 
        /// 比如需要执行javascript alert('Hello World!')
        /// 直接执行Response.Write(GetScript("alert('Hello World!')"));
        /// </summary>
        /// <param name="script">待运行的javascript脚本</param>
        /// <returns>服务器端发送的javascript脚本</returns>
        public static string GetScript(string script)
        {
            return string.Format("<script type=\"text/javascript\">{0}</script>", script);
        }

        /// <summary>
        /// 关闭当前页面，目前兼容IE6/7
        /// </summary>
        /// <param name="page">this</param>
        public static void ClosePage()
        {
            string script = "<script type=\"text/javascript\">window.opener = null;window.open('','_self');window.close();</script>";
            // page.ClientScript.RegisterClientScriptBlock(page.GetType(), "closepage", script);
            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 重新Post刷新父页面
        /// </summary>
        public static void ReloadParent()
        {
            string script = "<script type=\"text/javascript\">window.opener.opener = null;window.opener.document.forms[0].submit();</script>";
            HttpContext.Current.Response.Write(script);
        }

        /// <summary>
        /// 获取指定名称的cookie，若没有则返回string.Empty
        /// </summary>
        /// <param name="name">cookie name</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string name)
        {
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                if (HttpContext.Current.Request.Cookies[i].Name == name)
                {
                    return HttpContext.Current.Request.Cookies[i].Value;
                }
            }
            return "";
        }

        /// <summary>
        /// 清空cookie
        /// </summary>
        /// <param name="name">需要清除的cookie名称</param>
        public static void ClearCookie(string name)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Expires = DateTime.Today;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 设置一个cookie。
        /// </summary>
        /// <param name="name">cookie名称</param>
        /// <param name="value">cookie值</param>
        public static void SetCookie(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Value = value;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 设置一个cookie。
        /// </summary>
        /// <param name="name">cookie名称</param>
        /// <param name="value">cookie值</param>
        /// /// <param name="time">过期时间</param>
        public static void SetCookie(string name, string value, TimeSpan time)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Value = value;
            cookie.Expires = DateTime.Now.Add(time);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
